﻿using System;
using System.Collections.Generic;
using System.Text;

namespace gChartSharp
{
    /// <summary>
    /// http://code.google.com/apis/chart/#chart_data
    /// </summary>
    public class DataSeriesParam : IChartParam
    {
        /// <summary>
        /// The name of the API
        /// </summary>
        public string Name { get { return "chd"; } }

        /// <summary>
        /// The type of encoding
        /// </summary>
        public EncodingType EncodingType { get; set; }


        /// <summary>
        /// List of data sets
        /// </summary>
        public List<float[]> DataSets 
        {
            get { return mDataSets ?? (mDataSets = new List<float[]>()); }
            set { mDataSets = value; }
        }
        private List<float[]> mDataSets;


        /// <summary>
        /// Default constructor.  Default EncodingType is Text.
        /// </summary>
        public DataSeriesParam()
            : this(EncodingType.Text) { }


        /// <summary>
        /// Initialize the EncodingType
        /// </summary>
        /// <param name="encType"></param>
        public DataSeriesParam(EncodingType encType)
        {
            this.EncodingType = encType;
        }


        /// <summary>
        /// Initialize a DatSet
        /// </summary>
        /// <param name="dataSet"></param>
        public DataSeriesParam(float[] dataSet)
            : this(dataSet, EncodingType.Text) { }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataSet"></param>
        /// <param name="encType"></param>
        public DataSeriesParam(float[] dataSet, EncodingType encType)
        {
            this.EncodingType = encType;
            this.DataSets.Add(dataSet);
        }


        /// <summary>
        /// Render the text of the API
        /// </summary>
        /// <returns></returns>
        public string Render()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(String.Format("&{0}={1}:", this.Name, (char)this.EncodingType));

            if (this.DataSets.Count > 0)
            {
                foreach (float[] dataSet in this.DataSets)
                {
                    // Simple and Extended encoded data sets are delimited with a comma
                    if (this.EncodingType == EncodingType.Simple || this.EncodingType == EncodingType.Extended)
                    {
                        foreach (float f in dataSet)
                            sb.Append(String.Format("{0}", EncoderFactory.GetEncoderByType(this.EncodingType).Encode(f)));

                        // add delimiter
                        sb.Append(",");
                    }
                    // Text encoded data sets are delimited with a pipe, each data point is separated by a comma
                    else
                    {
                        foreach (float f in dataSet)
                            sb.Append(String.Format("{0},", EncoderFactory.GetEncoderByType(this.EncodingType).Encode(f)));

                        // remove trailing comma on data set
                        sb.Remove(sb.ToString().LastIndexOf(','), 1);

                        // add delimiter
                        sb.Append("|");
                    }
                }

                // remove trailing dataset delimiter
                if (this.EncodingType == EncodingType.Simple || this.EncodingType == EncodingType.Extended)
                    sb.Remove(sb.ToString().LastIndexOf(','), 1);
                else
                    sb.Remove(sb.ToString().LastIndexOf('|'), 1);
            }
            else 
            {
                // empty data set
                sb.Append('_');
            }

            return sb.ToString();
        }
    }
}
